package com.ssm.controller;

import com.ssm.pojo.User;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author  zxy
 * Date  2022/11/18 下午 2:46
 */
@Controller
// @RestController  // @Controller + @ResponseBody
public class TestAjaxController {

    @RequestMapping("/test/ajax")
    public void testAjax(Integer id, @RequestBody String requestBody, HttpServletResponse response) throws IOException {
        System.out.println("requestBody: " + requestBody);
        System.out.println("id:" + id);
        response.getWriter().write("hello, ajax");
    }

    // @RequestMapping("/test/RequestBody/json")
    public void testRequestBody(@RequestBody User user, HttpServletResponse response) throws IOException {
        System.out.println(user);
        response.getWriter().write("hello, RequestBody");
    }

    @RequestMapping("/test/RequestBody/json")
    public void testRequestBody(@RequestBody Map<String, Object> map, HttpServletResponse response) throws IOException {
        System.out.println(map);
        response.getWriter().write("hello, RequestBody");
    }

    @RequestMapping("/test/ResponseBody")
    @ResponseBody
    public String testResponseBody() {
        return "success";
    }

    @RequestMapping("/test/ResponseBody/json")
    @ResponseBody
    public List<User> testResponseBodyJson() {
        User user1 = new User(1001, "zxy", "ass", 23, "男");
        User user2 = new User(1002, "zxy", "ass", 23, "男");
        User user3 = new User(1003, "zxy", "ass", 23, "男");
        List<User> users = Arrays.asList(user1, user2, user3);
        return users;
    }
    /*public Map<String, Object> testResponseBodyJson() {
        User user1 = new User(1001, "zxy", "ass", 23, "男");
        User user2 = new User(1002, "zxy", "ass", 23, "男");
        User user3 = new User(1003, "zxy", "ass", 23, "男");
        Map<String, Object> map = new HashMap<>();
        map.put("1001", user1);
        map.put("1002", user1);
        map.put("1003", user1);
        return map;
    }*/
    /*public User testResponseBodyJson() {
        User user = new User(1001, "zxy", "ass", 23, "男");
        return user;
    }*/

}
